/**
 * 
 */
package com.p2p.front.web.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.p2p.base.model.BaseUser;
import com.p2p.base.model.Money;
import com.p2p.base.web.controller.BaseController;
import com.p2p.front.dal.model.TenderAutoSetting;
import com.p2p.front.service.TenderAutoService;
import com.p2p.trans.client.facade.AccountQueryFacade;
import com.p2p.trans.client.facade.AccountTransFacade;
import com.p2p.trans.client.result.AccountQueryResult;

/**
 * @author ricky
 *
 */
@RequestMapping("/tenderAuto")
@Controller
public class TenderAutoController extends BaseController{

	@Resource
	TenderAutoService tenderAutoService;
	@Resource
	AccountTransFacade accountTransFacade;
	
	@Resource
	AccountQueryFacade accountQueryFacade;
	
	@RequestMapping("/settingUpdate")
	public String tenderAutoSettingUpdate(ModelMap map,TenderAutoSetting tenderAutoSetting){
		BaseUser user = this.getCurrentUser();
		tenderAutoSetting.setUserId(user.getId());
		transformtendAutoSettingf2j(tenderAutoSetting);
		tenderAutoService.saveSetting(tenderAutoSetting);
		initPage(map,user);
		return "tenderAuto/setting";
	}
	@RequestMapping("/settingView")
	public String tenderAutoSettingView(ModelMap map){
		BaseUser user = this.getCurrentUser();
		initPage(map,user);
		return "tenderAuto/setting";
	}
	
	@RequestMapping("/open")
	public String open(ModelMap map,int open){
		BaseUser user = this.getCurrentUser();
		tenderAutoService.open(user.getId(), open);
		map.addAttribute("result", true);
		initPage(map,user);
		return "tenderAuto/setting";
	}
	
	
	private void initPage(ModelMap map,BaseUser user){
		TenderAutoSetting tenderAutoSetting  = tenderAutoService.getSetting(user.getId());
		/*boolean InQueue = tenderAutoService.checkInAutoQueue(user.getId());
		if(!InQueue) {
			//不在队列中，但是状态为1，则不现实排名
			if(tenderAutoSetting!=null&&tenderAutoSetting.getStatus()==1) {
				Money tendMoney = tenderAutoService.checkCanQueue(tenderAutoSetting);
				if(tendMoney!=null&&tendMoney.getCent()>0l){
//					this.inAutoQueue(tenderAutoSetting.getUserId());
					tenderAutoService.open(user.getId(), 1);
					map.addAttribute("rankID",tenderAutoService.getRankrdById(user.getId()));
				}else{
//					this.outAutoQueue(tenderAutoSetting.getUserId());
					map.addAttribute("rankID","不符合投标要求，无法显示排名");
				}
			}
		} else {  //如果在队列中，显示排名
			map.addAttribute("rankID",tenderAutoService.getRankrdById(user.getId()));
		}*/
		//如果用户设置了开启
		if(tenderAutoSetting!=null&&tenderAutoSetting.getStatus()!=null&&tenderAutoSetting.getStatus()==1) {
			boolean InQueue = tenderAutoService.checkInAutoQueue(user.getId());
			//不在队列中
			if(!InQueue) {
				Money tendMoney = tenderAutoService.checkCanQueue(tenderAutoSetting);
				if(tendMoney!=null&&tendMoney.getCent()>01) {	//如果用户可以进入队列
					//重新加入到队列中
					tenderAutoService.open(user.getId(),1);
					map.addAttribute("rankID",tenderAutoService.getRankrdById(user.getId()));
				} else {
					//用户不符合进入队列的要求
					map.addAttribute("rankID","不符合投标要求，无法显示排名");
				}
			} else {		//如果在队列中
				Money tendMoney = tenderAutoService.checkCanQueue(tenderAutoSetting);
				if(tendMoney!=null&&tendMoney.getCent()>01) {	//符合要求，显示排名
					map.addAttribute("rankID",tenderAutoService.getRankrdById(user.getId()));
				} else {
					//用户不符合进入队列的要求,则从队列中删除
					tenderAutoService.open(user.getId(), 1);
					map.addAttribute("rankID","不符合投标要求，无法显示排名");
				}
			}
		}
		if(tenderAutoSetting!=null) {
			transformtendAutoSettingj2f(tenderAutoSetting);
		}
		map.addAttribute("tenderAutoSetting", tenderAutoSetting);
		AccountQueryResult result = accountQueryFacade.getAccountInfo(user.getAccountNo());
		if(result.isSuccess()){
			map.addAttribute("accountVO", result.getAccountVO());
		}
	}
	
	//处理表单数据前台与后台的不一致性,从前台转换到后台
	private void transformtendAutoSettingf2j(TenderAutoSetting tenderAutoSetting) {
		//自动投标总金额
		Money totalAmount = tenderAutoSetting.getTotalAmount();
		//单次投标金额
		Money onceAmount = tenderAutoSetting.getOnceAmount();
		Money limitAmount = tenderAutoSetting.getLimitAmount();
		totalAmount.setCent(totalAmount.getCent()*100);
		onceAmount.setCent(onceAmount.getCent()*100);
		limitAmount.setCent(limitAmount.getCent()*100);
		tenderAutoSetting.setTotalAmount(totalAmount);
		tenderAutoSetting.setOnceAmount(onceAmount);
		tenderAutoSetting.setLimitAmount(limitAmount);
	}
	
	//处理表单数据前台与后台的不一致性,从后台转换到前台
		private void transformtendAutoSettingj2f(TenderAutoSetting tenderAutoSetting) {
			//自动投标总金额
			Money totalAmount = tenderAutoSetting.getTotalAmount();
			//单次投标金额
			Money onceAmount = tenderAutoSetting.getOnceAmount();
			Money limitAmount = tenderAutoSetting.getLimitAmount();
			Double minRate = tenderAutoSetting.getMinRate();//最小年回报率
			Double maxRate = tenderAutoSetting.getMaxRate();//最大年汇报率
			totalAmount.setCent(totalAmount.getCent()/100);
			onceAmount.setCent(onceAmount.getCent()/100);
			limitAmount.setCent(limitAmount.getCent()/100);
			tenderAutoSetting.setTotalAmount(totalAmount);
			tenderAutoSetting.setOnceAmount(onceAmount);
			tenderAutoSetting.setLimitAmount(limitAmount);
		}
}
